<?php
$shipping		= $_POST['shipping'];
$tax			= $_POST['tax'];
$taxRate		= $_POST['taxRate'];
$itemCount		= $_POST['itemCount'];

$qty = 0;
$prc = 0;

for($i = 1; $i <= $itemCount; $i++){
	$item_name[$i]	= $_POST["item_name_$i"];
	$item_quantity[$i]	= $_POST["item_quantity_$i"];
	$item_price[$i]	= $_POST["item_price_$i"];	
	$item_total_price[$i]	= $item_quantity[$i]*$item_price[$i];
	$qty			+= $item_quantity[$i];
	$prc			+= $item_quantity[$i]*$item_price[$i];
};

$first_name		= $_POST['first_name'];
$last_name		= $_POST['last_name'];
$nama			= $first_name." ".$last_name;
$email			= $_POST['email'];
$phone			= $_POST['phone'];
$address		= $_POST['address'];
$postcode		= $_POST['postcode'];

$shop_name		= $_POST['shop_name'];
$shop_url		= $_POST['shop_url'];
$shop_thankyou	= $_POST['shop_thankyou'];
$shop_sendFail	= $_POST['shop_sendFail'];
$shop_email		= $_POST['shop_email'];
$shop_currency	= $_POST['shop_currency'];
$shop_bizname	= $_POST['shop_bizname'];
$shop_bizaddress	= $_POST['shop_bizaddress'];
$shop_bizphone1	= $_POST['shop_bizphone1'];
$shop_bizphone2	= $_POST['shop_bizphone2'];
$shop_bizphone3	= $_POST['shop_bizphone3'];

$mandiri_acc_no	= $_POST['mandiri_acc_no'];
$mandiri_acc_name	= $_POST['mandiri_acc_name'];
$bca_acc_no		= $_POST['bca_acc_no'];
$bca_acc_name	= $_POST['bca_acc_name'];
$bri_acc_no		= $_POST['bri_acc_no'];
$bri_acc_name	= $_POST['bri_acc_name'];
$bni_acc_no		= $_POST['bni_acc_no'];
$bni_acc_name	= $_POST['bni_acc_name'];

$host_mail	= "ssl://smtp.gmail.com";
$port_smtp	= "465";
$mail_user	= "didiktri93";
$mail_pass	= "anggahuda93";
$mail_email	= "didiktri93@gmail.com";

function rupiah($nilai) {
    return number_format($nilai, 2, '.', ',');
}

require_once('https://checkout-pribadi.googlecode.com/svn/class.phpmailer.php'); //menginclude librari phpmailer

$mail = new PHPMailer();
$body = "
<html>
    <body>
        <div style='display:block;padding:5%;width:90%;background:#f0f0f0;'>
        	<h1 style='color: #E74C3C;text-shadow: 2px -2px 0px #333;'>".$shop_name."</h1>
            <div style='display:block;margin:0 auto;padding:20px;background:#fff;border-radius:8px;position:relative;box-shadow:0px 0px 15px #333;border-top:15px solid #06c;border-bottom:15px solid #06c;'>
                Terimakasih Bapak/Ibu ".$nama." telah berbelanja di ".$shop_name.".
                <br />
                <h3 style='color: #23A1D1;'>Data Pembeli</h3>
				<table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='0' cellspacing='0'>
				    <tbody><tr>
				            <td style='width:600px;max-width:100%' valign='top'>
				                <strong style='color:#444'>".$nama."</strong><br>
				                <strong style='color:#444'>".$phone."</strong><br>
				            </td>
				            <td style='width:600px;max-width:100%' valign='top'>
				                Dusun Merjoyo <br>
				                RT 1 RW 9 (Gang Depan Masjid) <br>
				                Sukodadi <br>
				                Jawa Timur - Sukodadi <br>
				                62253 </td>
				        </tr>
				    </tbody>
				</table>
                <br />
                <h3 style='color: #23A1D1;'>Barang yang dipesan</h3>
                <table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='20' cellspacing='0'>
                	<tr>
                		<td style='border:1px solid #ddd;width:600px;max-width:100%' valign='top' align='center'>
							<table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='0' cellspacing='0'>
			                    <tbody>
			                        <tr>
			                            <td><span style='width:25%;display:block;font-family:Helvetica;font-size:15px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>Barang</span></td>
			                            <td><span style='width:25%;display:block;font-family:Helvetica;font-size:15px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>Harga</span></td>
			                            <td><span style='width:25%;display:block;font-family:Helvetica;font-size:15px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>Qty</span></td>
			                            <td><span style='width:25%;display:block;font-family:Helvetica;font-size:15px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>Total</span></td>
			                        </tr>";
for($i = 1; $i <= $itemCount; $i++){ 
$body .= "
			                        <tr>
				                        <td><strong>".$item_name[$i]."</strong></td>
				                        <td><strong>Rp&nbsp;".rupiah($item_price[$i])."</strong></td>
				                        <td><strong>".$item_quantity[$i]."</strong></td>
				                        <td><strong>Rp&nbsp;".rupiah($item_total_price[$i])."</strong></td>
			                        </tr>";
};
$body .= "
			                    </tbody>
			                </table>
                		</td>
                	</tr>
                </table>
                
                <br />
                <h3 style='color: #23A1D1;'>Rinci biaya pesanan</h3>
                <table style='border-collapse:collapse!important' border='0' cellpadding='0' cellspacing='0'>
                	<tbody>
                        <tr>
                            <td align='right' style='padding-right: 10px'>Total Qty:</td>
                            <td><b>".$qty."</b></td>
                        </tr>
                        <tr>
                            <td align='right' style='padding-right: 10px'>Biaya belanja:</td>
                            <td><b>Rp&nbsp;<span style='float: right;'>".rupiah($prc)."</span></b></td>
                        </tr>
                        <tr>
                            <td align='right' style='padding-right: 10px'>Biaya pengiriman:</td>
                            <td><b>Rp <span style='float: right;'>0.00</span></b></td>
                        </tr>
                        <tr style='background:#f0f0f0;'>
                            <td align='right' style='padding-right: 10px'><b>Total biaya:</b></td>
                            <td><b style='color:#23A1D1'>Rp&nbsp;<span style='float: right;'>".rupiah($prc)."</span></b></td>
                        </tr>
                    </tbody>
                </table>
                <br />
                <h3 style='color: #23A1D1;'>Pembayaran melalui</h3>
                Pesanan Anda akan segera diproses setelah melakukan pembayaran melalui nomor rekening resmi kami yang tertera di bawah ini. <br /><br />
				<table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='0' cellspacing='0'>
				    <tbody><tr>
				            <td style='border:1px solid #ddd;width:600px;max-width:100%' valign='top' align='center'>
				                <table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='10' cellspacing='0'>
				                    <tbody><tr>
				                            <td style='text-align:center' valign='top'>
				                                <img class='CToWUd' src='https://googledrive.com/host/0ByIiSD8UcStZMXAxaWdKSkFKcjA' alt='BCA' style='border:0;min-height:auto;line-height:100%;outline:none;text-decoration:none;display:inline;max-width:560px'> <br>
				                                <strong style='color:#444'>".$bca_acc_no."</span></strong>
				                                <h4 style='display:block;font-family:Helvetica;font-size:12px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>
				                                    a/n ".$bca_acc_name." <br>
				                                </h4>
				                            </td>
				                        </tr>
				                    </tbody>
				                </table>
				            </td>
				            <td style='border:1px solid #ddd;width:600px;max-width:100%' valign='top' align='center'>
				                <table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='10' cellspacing='0'>
				                    <tbody><tr>
				                            <td style='text-align:center' valign='top'>
				                                <img class='CToWUd' src='https://googledrive.com/host/0ByIiSD8UcStZa2RiazlDTW5tX0k' alt='Mandiri' style='border:0;min-height:auto;line-height:100%;outline:none;text-decoration:none;display:inline;max-width:560px'> <br>
				                                <strong style='color:#444'>".$mandiri_acc_no."</span></strong>
				                                <h4 style='display:block;font-family:Helvetica;font-size:12px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>
				                                    a/n ".$mandiri_acc_name." <br>
				                                </h4>
				                            </td>
				                        </tr>
				                    </tbody>
				                </table>
				            </td>
				            <td style='border:1px solid #ddd;width:600px;max-width:100%' valign='top' align='center'>
				                <table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='10' cellspacing='0'>
				                    <tbody><tr>
				                            <td style='text-align:center' valign='top'>
				                                <img class='CToWUd' src='https://googledrive.com/host/0ByIiSD8UcStZdzZ3OTF0VFBIS00' alt='BNI' style='border:0;min-height:auto;line-height:100%;outline:none;text-decoration:none;display:inline;max-width:560px'> <br>
				                                <strong style='color:#444'>".$bni_acc_no."</span></strong>
				                                <h4 style='display:block;font-family:Helvetica;font-size:12px;font-weight:normal;line-height:100%;letter-spacing:normal;margin-top:0;margin-right:0;margin-bottom:3px;margin-left:0;color:#808080!important'>
				                                    a/n ".$bni_acc_name." <br>
				                                </h4>
				                            </td>
				                        </tr>
				                    </tbody>
				                </table>
				            </td>
				        </tr>
				    </tbody>
				</table> 
                <br />
                <div style='clear:both;'></div>
            </div>
            <table style='border-collapse:collapse!important' width='100%' border='0' cellpadding='0' cellspacing='0'>
                <tbody>
                    <tr>
                        <td>
                            <a style='color:#000;' href='".$shop_url."'><b>Online Store Managed By:</b></a><br />
                            <b>Jualan</b><br />
                            <i>Bangkalan, Indonesia</i>
                        </td>
                        <td style='text-align:right;border:0;'>
                            <b>Contact Us:</b><br />
                            <i>017-123 4567</i>&nbsp;&nbsp;<br />
                            Email: 
                            <span style='color:#333;'>".$shop_email."</span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>";

$body = eregi_replace("[\]", '', $body);
$mail->IsSMTP();     // menggunakan SMTP
$mail->SMTPDebug = 1;   // mengaktifkan debug SMTP

$mail->SMTPAuth = true;   // mengaktifkan Autentifikasi SMTP
$mail->Host = $host_mail; // host sesuaikan dengan hosting mail anda
$mail->Port = $port_smtp;  // post gunakan port 25
$mail->Username = $mail_user; // username email akun
$mail->Password = $mail_pass;        // password akun

$mail->From = $mail_email;
$mail->FromName = $shop_name;

$mail->Subject = "Nota Pembelian (INVOICE)";
$mail->MsgHTML($body);

$mail->AddAddress($email, $nama);

if (!$mail->Send()) {
	echo "<script>document.location.replace('//".$shop_sendFail."');</script>";
} else {
	echo "<script>document.location.replace('//".$shop_thankyou."');</script>";
}
?>